﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GTA
{
    public class Menu
    {
        // Menu elements
        Texture2D background;
        int selectedMenu = 0;
        bool canMove = true; // Prevent from spamming down/up key

        // Explosion
        Texture2D explo;
        Vector2 exploPos;
        Point exploSize, exploNumber, currentexplo;
        Rectangle exploSelected;
        int exploFps, exploTimer;

        // Main Screen elements
        Texture2D mainMenu1, mainMenu2, mainMenu3, mainMenu4;
        Vector2 mainMenuPos1, mainMenuPos2, mainMenuPos3, mainMenuPos4;

        // Which screen menu is activated
        public enum menuScreen
        {
            Main, // Main Screen
            Option,
            Option1,
            Option2,
            Option3,
            Fame,
            Pause,
            None // Ingame
        }

        // Current menu screen
        public menuScreen Screen
        {
            get { return screen; }
            set { screen = value; }
        }
        menuScreen screen = menuScreen.Main;

        public Menu()
        {

        }

        public void InitMenu(ContentManager Content)
        {
            InitMisc(Content);
            InitMainScreen(Content);
        }

        public void Update(Game1 game)
        {
            if (screen != menuScreen.Pause && screen != menuScreen.None)
            {
                UpdateMisc();
                UpdateMainScreen(game);
            }
        }

        public void DrawMenu(SpriteBatch spriteBatch)
        {
            if (screen != menuScreen.Pause && screen != menuScreen.None)
            {
                spriteBatch.Draw(background, Vector2.Zero, Color.White);
                spriteBatch.Draw(explo, exploPos, exploSelected, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0);
                DrawMainScreen(spriteBatch);

                // Debug
                spriteBatch.DrawString(Game1.font, ""+currentexplo, new Vector2(0,0), Color.Red);
            }
        }

        /* Custom functions */
        private void ResetMenu()
        {
            exploPos = exploPos = new Vector2(135, 195);
            exploSelected = new Rectangle(0, 0, exploSize.X, exploSize.Y);
            currentexplo = new Point(0, 0);
            canMove = false;
            selectedMenu = 0;

        }

        private void InitMisc(ContentManager Content)
        {
            background = Content.Load<Texture2D>("Menu/Misc/menuBack");

            explo = Content.Load<Texture2D>("Menu/Misc/explo");
            exploFps = 5;
            exploTimer = 0;
            exploSize = new Point(65, 78);
            exploNumber = new Point(13, 3);
            exploPos = new Vector2(135, 195);
            currentexplo = new Point(0, 0);
            exploSelected = new Rectangle(0, 0, 0, 0);
        }

        private void UpdateMisc()
        {
            exploTimer++;

            // Explosion animation
            if (exploTimer > exploFps)
            {
                exploTimer = 0;
                ++currentexplo.X;

                if (currentexplo.X > exploNumber.X - 1)
                {
                    currentexplo.X = 0;
                    ++currentexplo.Y;

                    if (currentexplo.Y > exploNumber.Y - 1)
                    {
                        currentexplo.Y = 0;
                    }
                }

            }

            exploSelected = new Rectangle(currentexplo.X * exploSize.X, currentexplo.Y * exploSize.Y, exploSize.X, exploSize.Y);

            if (!canMove && Keyboard.GetState().IsKeyUp(Keys.Up) && Keyboard.GetState().IsKeyUp(Keys.Down))
                canMove = true;
           
        }

        private void InitMainScreen(ContentManager Content)
        {
            mainMenuPos1 = new Vector2(210, 200);
            mainMenuPos2 = new Vector2(210, 300);
            mainMenuPos3 = new Vector2(210, 400);
            mainMenuPos4 = new Vector2(210, 500);

            mainMenu1 = Content.Load<Texture2D>("Menu/Main/mainMenu1");
            mainMenu2 = Content.Load<Texture2D>("Menu/Main/mainMenu2");
            mainMenu3 = Content.Load<Texture2D>("Menu/Main/mainMenu3");
            mainMenu4 = Content.Load<Texture2D>("Menu/Main/mainMenu4");
        }
        
        private void UpdateMainScreen(Game1 game)
        {
            // Allow to choose what menu we want with the keys
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && canMove)
            {
                if (selectedMenu == 3)
                {
                    selectedMenu = 0;
                    exploPos = new Vector2(135, 195);
                }
                else
                {
                    selectedMenu++;
                    exploPos.Y += 100;
                }
                canMove = false;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Up) && canMove)
            {
                if (selectedMenu == 0)
                {
                    selectedMenu = 3;
                    exploPos = new Vector2(135, 495);
                }
                else
                {
                    selectedMenu--;
                    exploPos.Y -= 100;
                }
                canMove = false;
            }

            // Play
            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && selectedMenu == 0)
            {
                ResetMenu();
                Game1.State = Game1.GameState.Ingame;
                screen = menuScreen.None;
            }

            // Quit
            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && selectedMenu == 3)
            {
                game.Exit();
            }
        }

        private void DrawMainScreen(SpriteBatch spriteBatch)
        {
            if (screen != menuScreen.Main)
                return;

            spriteBatch.Draw(mainMenu1, mainMenuPos1, Color.White);
            spriteBatch.Draw(mainMenu2, mainMenuPos2, Color.White);
            spriteBatch.Draw(mainMenu3, mainMenuPos3, Color.White);
            spriteBatch.Draw(mainMenu4, mainMenuPos4, Color.White);
        }
    }
}
